/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.ad.service.impl;

import me.zhengjie.modules.app.ad.domain.AduserXScreen;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.ad.repository.AduserXScreenRepository;
import me.zhengjie.modules.app.ad.service.AduserXScreenService;
import me.zhengjie.modules.app.ad.service.dto.AduserXScreenDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXScreenQueryCriteria;
import me.zhengjie.modules.app.ad.service.mapstruct.AduserXScreenMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Haojie Li
* @date 2021-04-09
**/
@Service
@RequiredArgsConstructor
public class AduserXScreenServiceImpl implements AduserXScreenService {

    private final AduserXScreenRepository aduserXScreenRepository;
    private final AduserXScreenMapper aduserXScreenMapper;

    @Override
    public Map<String,Object> queryAll(AduserXScreenQueryCriteria criteria, Pageable pageable){
        Page<AduserXScreen> page = aduserXScreenRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(aduserXScreenMapper::toDto));
    }

    @Override
    public List<AduserXScreenDto> queryAll(AduserXScreenQueryCriteria criteria){
        return aduserXScreenMapper.toDto(aduserXScreenRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AduserXScreenDto findById(String id) {
        AduserXScreen aduserXScreen = aduserXScreenRepository.findById(id).orElseGet(AduserXScreen::new);
        ValidationUtil.isNull(aduserXScreen.getId(),"AduserXScreen","id",id);
        return aduserXScreenMapper.toDto(aduserXScreen);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AduserXScreenDto create(AduserXScreen resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return aduserXScreenMapper.toDto(aduserXScreenRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AduserXScreen resources) {
        AduserXScreen aduserXScreen = aduserXScreenRepository.findById(resources.getId()).orElseGet(AduserXScreen::new);
        ValidationUtil.isNull( aduserXScreen.getId(),"AduserXScreen","id",resources.getId());
        aduserXScreen.copy(resources);
        aduserXScreenRepository.save(aduserXScreen);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            aduserXScreenRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AduserXScreenDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AduserXScreenDto aduserXScreen : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("广告商id", aduserXScreen.getAdUserId());
            map.put("屏保图片地址", aduserXScreen.getScreenUrl());
            map.put("创建者", aduserXScreen.getCreateBy());
            map.put("更新者", aduserXScreen.getUpdateBy());
            map.put("创建时间", aduserXScreen.getCreateTime());
            map.put("更新时间", aduserXScreen.getUpdateTime());
            map.put("数据状态", aduserXScreen.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}